package com.cjxt.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjxt.ams.dao.MenuDao;
import com.cjxt.ams.domain.*;
import com.cjxt.ams.service.MenuService;
import com.cjxt.ams.service.RoleMenuService;
import com.cjxt.ams.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhanghuan
 * @version V1.0
 * @Description: 系统菜单 业务层实现类
 * @className: MenuServiceImpl
 * @date 2021/11/22 15:36
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Resource
    private MenuDao menuDao;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleMenuService roleMenuService;

    // 根据菜单ID删除菜单
    @Transactional
    @Override
    public void delete(Integer menuId) {
        this.removeById(menuId);
        // 删除菜单与角色关联
        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("menu_id", menuId));
    }

    // 根据父菜单ID，查询子菜单
    @Override
    public List<Menu> queryListMenuPid(Integer menuPid) {
        return baseMapper.queryListMenuPid(menuPid);
    }

    @Override
    public List<TreeNode> treeData() {
        List<TreeNode> list = new LinkedList<>();
        List<Menu> menus  = menuDao.queryTreeData();
        if (menus != null) {
            for (Menu menu: menus) {
                list.add(new TreeNode(menu.getId(),menu.getMenuName(),menu.getMenuPid(),null ));
            }
        }
        return list;
    }

    public List<Menu> queryTreeUser(Integer userId){
        List<Integer> ids = userService.queryAllMenuId(userId);
        List<Menu> menus = new LinkedList<>();
        for (Integer id : ids) {
            Menu menu = menuDao.queryTreeDataUser(id);
            menus.add(menu);
        }
        return menus;
    }

    @Override
    public List<TreeNodeUser> treeDataUser(Integer userId) {
        LinkedList<TreeNodeUser> list = new LinkedList<>();
        List<Menu> menus  = queryTreeUser(userId);
        if (menus != null) {
            for (Menu menu: menus) {
                list.add(new TreeNodeUser(menu.getMenuName(),menu.getIcon(),"false",menu.getPath(),null,menu.getId(),menu.getMenuPid()));
            }
        }
        return list;
    }

    @Override
    public List<Menu> queryList(List<Menu> menuList){
        for (Menu menu : menuList) {
            Integer pid = menu.getMenuPid();
            if(pid == 0){
                menu.setParentName("一级菜单");
            }else{
                Menu menuPid = menuDao.selectOne(new QueryWrapper<Menu>().eq("id", pid));
                menu.setParentName(menuPid.getMenuName());
            }
        }
        return menuList;
    }

}